﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIScene : UIDialog 
{
	public Dictionary<int, UINode> uiNodeList = new Dictionary<int, UINode> ();
	
	void Start () 
	{
		var listUINode = MapData.Instance.Map;

		float posX = 0;
		float posY = 0;

		float gridWidth = 0;
		float gridHeight = 0;
		for (var i = 0; i < Const.MapHeight; i++) 
		{
			for (var j = 0; j < Const.MapWidth; j++) 
			{
				if(listUINode[i,j]==null)
				{
					posX = 0;
					posY -= gridHeight;
					continue;
				}
				UINode uiNode = (UINode)addChild (new UINode());
				uiNode.bindData (listUINode[i,j]);
				uiNode.keyX = listUINode[i,j].PosX;
				uiNode.keyY = listUINode[i,j].PosY;
				uiNodeList.Add (uiNode.keyX * 10000 + uiNode.keyY, uiNode);

				if (j % 2 == 0) 
				{
					uiNode.setPostion (posX,posY);
				} 
				else 
				{
					uiNode.setPostion (posX,posY);
				}
					
				gridWidth = uiNode.getRectTransform ().sizeDelta.x;
				gridHeight = uiNode.getRectTransform ().sizeDelta.y;

				if (j == Const.MapWidth - 1) 
				{
					if (i % 2 == 0)
						posX = gridWidth/ 2;
					else
						posX = 0;
					posY -= gridHeight;
				} 
				else 
				{
					posX += gridWidth;
				}
			}
		}
	}

	public UINode getNodeByXY(int x, int y)
	{
		return uiNodeList [x * 10000 + y];
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}

	public override string path ()
	{
		return "UIDialog/UIScene";
	}
}
